package main

import "fmt"

func main() {
	nums := []int{1, 2, 3, 4}
	v := productExceptSelf(nums)
	fmt.Println(v)
}

func productExceptSelf(nums []int) []int {
	n := len(nums)
	res := make([]int, n)
	res[0] = 1

	otherVal := 1
	for i := 1; i < n; i++ {
		otherVal *= nums[i-1]
		res[i] = otherVal
	}
	otherVal = 1
	for i := n - 2; i >= 0; i-- {
		otherVal *= nums[i+1]
		res[i] *= otherVal
	}
	return res
}
